$(function() {
// 获取用户评价的列表
  function layerCss(){
    $(".layui-layer").css("border-radius","10px")
    $(".layui-layer-setwin .layui-layer-close2").css({"right":"-40px","top":"0px"});
    $(".layui-layer-ico").css("background","url("+jumpUrl+"/img/question2/iconlayer.png)");
  }
  var index = $(".load-more").attr("data-id");
  if (index == "1") {index = ""};
  var lawyer_id = id;

  // 请求评价标签数量
   get_evaluate_mun();
  function get_evaluate_mun(){
    var evaluate_url = apitest+"/evaluate/"+lawyer_id; 
    $.get(evaluate_url, function(data){
      }, "json").error(function(data) {
        var status_code = $.parseJSON(data.responseText);
        layer.msg(status_code.message);
      }).success(function(data) {
        $(".loading").remove();
         var obj = data.data.extras;
         console.info(obj);
         $(".high").text(obj.high);
         $(".intermediate").text(obj.intermediate);
         $(".low").text(obj.low);
       })
  };

// 请求评价列表
  get_evaluate();
  function get_evaluate(){
    var evaluate_type = $(".evaluate-but a.cua").attr("data-id");
    if (evaluate_type=="all") {
      var evaluate_url = apitest+"/evaluate/"+lawyer_id; 
    }else{
      var evaluate_url = apitest+"/evaluate/"+lawyer_id+"?score="+evaluate_type;
    }
    var loading = '<div class="loading">加载中...</div>';
    $(".good-evaluate").append(loading);
    $.get(evaluate_url, function(data){
      }, "json").error(function(data) {
        var status_code = $.parseJSON(data.responseText);
        layer.msg(status_code.message);
      }).success(function(data) {
        $(".loading").remove();
         var obj = data.data.list;
         console.info("obj",obj);
         var str = '';
         if (obj.length>7) {
            $(".load-more").css("display","block")
          }else{
            $(".load-more").css("display","none")
          }
        if (obj.length>0) {
           for(var i = 0;i < obj.length; i++){
            
            var tags_length = obj[i].tags.length;
            if (obj[i].realname==null) {obj[i].realname="普通用户"};
            str +='<li>'
                 str +='<div class="fl box1 font16">'
                   str +='<p class="colorD oh"><span>'+obj[i].realname+'</span>&nbsp;&nbsp;&nbsp;&nbsp;<span>'+obj[i].difference_date+'</span><span class="fr font18"><span class="colorB">'+obj[i].score+'</span> 分</span></p>'
                   str +='<br>'
                   str +='<p class="font18 colorE" style="line-height: 30px">'+obj[i].comment+'</p>'
                   str +='<p class="evaluate-type">'
                  for (var n = 0; n < tags_length; n++) {
                    if (obj[i].tags[n]==null) {
                     str +=''
                    }else{
                      str +='<a href="">'+obj[i].tags[n]+'</a>'
                    }
                  } 
                   str +='</p>'
                   str +='<p class="colorD font14" style="margin-top:15px;">'+obj[i].post_type_name+'</p>'
                 str +='</div>'
               str +='</li>'
            }
        }else{
          str ='<li class="textac"><span class="noData2"></span></li>';
        }
        $(".good-evaluate").append(str);
      });
    }

   // 加载更多
    $(document).on("click",".load-more",function(e){
      index = parseInt($(".load-more").attr("data-id"))+1;
      $(".load-more").attr("data-id",index);
      get_evaluate();
    });

  // 点击切换评价列表
  $(".evaluate-but a").click(function(){
    $(".evaluate-but a").removeClass("cua");
    $(".good-evaluate li").remove();
    $(this).addClass("cua");
    var evaluate_type =  $(this).attr("data-id");
    var evaluate_url = jumpUrl+'/lawyer/workscopes?'+evaluate_type;
    get_evaluate();
  });

  //点击关注
  $(document).on('click', '.follow-but', function(e) {
    if(islogin == "" || islogin == null){
      layer.msg("请登录后在关注！");
      
      return false;
    }else{
      var loading = layer.msg('操作进行中...', {icon: 16});
      var follow_id = $(this).attr("data-id");
      var token = $("input:[name='_token']").val();
      var params = {};
      params['follow_id'] = follow_id;
      params['user_id'] = user_id;
      params['_token'] = token;
      var url = jumpUrl+'/follow';
      $.post(url,params,function(data){

        }, "json").error(function(data) {
          var error =  $.parseJSON(data.responseText);
          $.each(error.error_messages, function(i, item){
            layer.msg(item[0]);
          });
        }).success(function(data) {
          var obj = data.data;
          layer.msg(obj);
          $('.follow-but').text("取消关注");
          $('.follow-but').attr("class","unfollow");
        })
    }
    stopDefault(e);
  });
    //取消关注
  $(document).on('click', '.unfollow', function(e) {
    if(islogin == "" || islogin == null){
      layer.msg("请登录后在关注！");
      return false;
    }else{
      var loading = layer.msg('操作进行中...', {icon: 16});
      var $this = $(this);
      var follow_id = $(this).attr("data-id");
      var token = $("input:[name='_token']").val();
      var params = {};
      params['follow_id'] = follow_id;
      params['user_id'] = user_id;
      params['_token'] = token;
      var url = jumpUrl+'/follow/remove';
      $.post(url,params,function(data){
        
        }, "json").error(function(data) {
          var error =  $.parseJSON(data.responseText);
          $.each(error.error_messages, function(i, item){
            layer.msg(item[0]);
          });

        }).success(function(data) {
          var obj = data.data;
          layer.msg(obj);
          $('.unfollow').text("点击关注");
          $('.unfollow').attr("class","follow-but");
        })
      }
      stopDefault(e);
    });


  // 获取律师文章
  get_article();
  function get_article(){
    // var evaluate_url = apitest+"/question?type=question&size=8";
    var evaluate_url = jumpUrl+"/lawyer/" + id + "/articles";
    var loading = '<div class="loading" style="margin-top: 20px;">加载中...</div>';
    $(".lv-article").append(loading);
    $.get(evaluate_url, function(data){
      }, "json").error(function(data) {
        var status_code = $.parseJSON(data.responseText);
        layer.msg(status_code.message);
      }).success(function(data) {
        $(".loading").remove();
         var obj = data.data;
         console.info(obj);
         var str = '';
        if (obj.length<1) {
          $(".view-content2").css("display","none");
        }
        if (obj.length>0) {
           for(var i = 0;i < obj.length; i++){
            str +='<li>'
               str +='<p class="font16" style="line-height:20px"><a class="colorF" href="'+jumpUrl+'/knowledge/'+obj[i].id+'/view">'+obj[i].title+'</a></p>'
               str +='<p class="evaluate-type colorD">'+obj[i].created_at+'</p>'
            str +='</li>'
            }
        }else{
          str ='<li class="textac"><span class="colorB">暂无数据</span></li>';
        }
        $(".lv-article").append(str);
    });
  }

    $(".advisory1").click(function () {
      var question_types = $(this).attr("data-id");
      localStorage.setItem("question_type", JSON.stringify(question_types));
      console.info(timestamp);
      window.location.href=jumpUrl+'/question/fill_in_view/'+id+'?'+timestamps;
    });
  /*登录弹窗*/
  $(".service-but").click(function(){
    layer.closeAll();
    var Popup = $("#serviceDiv");
        layer.open({
          type: 1, //基本层类型  可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）。 若你采用layer.open({type: 1})方式调用，则type为必填项（信息框除外）
          title: false, //标题
          closeBtn: true,  //关闭按钮
          shade:0.3,  //遮罩默认是0.3透明度
          area: ['422px', '376px'],
          skin: 'yourclass', //样式类名
          content: Popup //内容 
    });
    layerCss();
  });


});
